package com.mobdev.notepad.shared;

import android.app.Activity;
import android.text.Spannable;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public abstract class AbstractSyncActivity extends Activity {

	/**  The sync thread. */
	protected AbstractSyncThread syncThread = null;

	/** Sync conflict button panel */
    protected View buttonPanel = null;

    protected Button button0 = null;
    protected Button button1 = null;
    protected Button button2 = null;

	protected TextView textView = null;


	/**
     * Called when activity is paused.
     */
    @Override
    protected void onPause()
    {
    	if(syncThread != null && isFinishing()) {
    		syncThread.requestStop();
    	}

    	super.onPause();
    }


    /**
     * Shows the button panel and sets the buttons.
     */
    public void showButtonPanel(String[] buttonText, OnClickListener buttonListener)
    {
		button0.setText(buttonText[0]);
		button0.setOnClickListener( buttonListener );
		button0.invalidate();
		button0.setVisibility( View.VISIBLE );

		if(buttonText.length > 1)
		{
			button1.setText(buttonText[1]);
			button1.setOnClickListener( buttonListener );
			//button1.invalidate();
			button1.setVisibility( View.VISIBLE );

		}
		else
		{
			button1.setVisibility( View.GONE );
		}

		if(buttonText.length > 2)
		{
			button2.setText(buttonText[2]);
			button2.setOnClickListener( buttonListener );
			//button2.invalidate();
			button2.setVisibility( View.VISIBLE );
		}
		else
		{
			button2.setVisibility( View.GONE );
		}

        buttonPanel.setVisibility(View.VISIBLE);
        buttonPanel.requestLayout();

    }

	public void setText(String mediumText, String smallText) {
		String text = "";
		if(mediumText != null)
			text += mediumText;
		if(smallText != null)
			text += smallText;

		textView.setText(text);

		if(mediumText != null)
			( (Spannable) textView.getText()).setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium), 0, mediumText.length(), 0);

		if(smallText != null)
			( (Spannable) textView.getText()).setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Small), mediumText.length(), mediumText.length() + smallText.length(), 0);

	}

	public abstract void showPreferences();

	 /**
     * Hides the button panel
     */
    public void hideButtonPanel()
    {
    	buttonPanel.setVisibility(View.GONE);
    }

	public abstract String getLocalyticsAppKey();

}
